package com.nema.test;

import com.nema.infer.NemaInfer;
import com.nema.util.QueryByTitle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by Wang on 2017/6/2.
 */
@Service
public class Tool {
	int LEVEL = 2000;

	public void score(String str) throws Exception {
		Map<String, String> info;
		Map<String, String> input = new HashMap<String, String>();
		 String[] attrib = {"score", "timespan",
		 "sale_effiency", "country", "education", "certification",
		 "level"};
		int x, y, z, i;
		String temp;
		Random rand = new Random();

		QueryByTitle qbt = new QueryByTitle();
		info = qbt.query(str);
		input.put("gig", str);

		temp = info.get("duration");
		i = rand.nextInt(3) + 1;
		x = Integer.parseInt(temp.split("-")[0]);
		y = Integer.parseInt(temp.split("-")[1]);
		z = (y - x) / 3;
		input.put("duration", (x + i * z) + "");

		temp = info.get("price");
		i = rand.nextInt(3) + 1;
		x = Integer.parseInt(temp.split("-")[0]);
		y = Integer.parseInt(temp.split("-")[1]);
		z = (y - x) / 3;
		input.put("price", (x + i * z) + "");

		temp = info.get("avg_resp_time");
		i = rand.nextInt(3) + 1;
		x = Integer.parseInt(temp.split("-")[0]);
		y = Integer.parseInt(temp.split("-")[1]);
		z = (y - x) / 3;
		input.put("avg_resp_time", (x + i * z) + "");

		temp = info.get("language");
		String[] arr = temp.split(";");
		i = rand.nextInt(arr.length);
		input.put("language", arr[i]);

		temp = info.get("tag");
		arr = temp.split(";");
		i = rand.nextInt(arr.length);
		input.put("tag", arr[i]);

		for(String attr : attrib){
			input.put(attr, "-1");
		}

		double r1 = System.currentTimeMillis()/1000.0;
		NemaInfer nema = new NemaInfer(input);
		nema.inference();
		String res = nema.printOPT();
		double r2 = System.currentTimeMillis()/1000.0;

		int[] good = new int[3];
		i = 0;
		int j = 0, k = 0;

		int cnt = res.split("-")[1].split(";").length;

		if(cnt >= 10) try(Connection conn = dataSource.getConnection()){
			for (String gid : res.split("-")[1].split(";")) {
				PreparedStatement preparedStatement = conn.prepareStatement(
				"select score from gig where gig_id='" + gid + "'");
				ResultSet ret = preparedStatement.executeQuery();
				if (ret.next()) {
					float tmp = ret.getFloat(1);
					if(i++ < 10){
						if (tmp >= 75)
							good[2]++;
						if(j++ < 5){
							if (tmp >= 75)
								good[1]++;
							if(k++ < 3){
								if (tmp >= 75)
									good[0]++;
							}
						}
					}else{
						break;
					}
				}
			}


			conn.prepareStatement("insert into topm"+LEVEL+" (title,t3,t5,t10,runtime,ratio,count) values('" + str
					+ "', '" + good[0] + "', '" + good[1] + "', '" + good[2]+ "', '" + (r2-r1) + "', '" + (cnt/LEVEL) + "', '" + cnt + "')").executeUpdate();
		}

	}

	@Autowired
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}
